package deltax.common.resource
{
    import deltax.common.log.*;
    import flash.utils.*;

    final public class DownloadStatistic extends Object
    {
        private var m_totalDownloadedBytes:uint;
        private var m_downloadedBytesFromLastCheck:uint;
        private var m_lastCheckTime:uint;
        private var m_downloadBytesPerSecond:Number = 0;
        private var m_debug:Boolean;
        private var m_maxDownloadSpeed:Number;
        private var m_minDownloadSpeed:Number = 1.79769e+308;
        private var m_avgDownloadSpeed:Number = 0;
        private static const UPDATE_STATISTIC_INTERVAL:uint = 1000;
        private static var m_instance:DownloadStatistic;

        public function DownloadStatistic(param1:SingletonEnforcer)
        {
            this.m_maxDownloadSpeed = Number.MIN_VALUE;
            this.m_lastCheckTime = getTimer();
            return;
        }// end function

        public function set debug(param1:Boolean) : void
        {
            this.m_debug = param1;
            return;
        }// end function

        public function addDownloadedBytes(param1:uint, param2:String = null) : void
        {
            if (this.m_debug)
            {
                dtrace(LogLevel.INFORMATIVE, "new download done: " + param1, param2);
            }
            this.m_totalDownloadedBytes = this.m_totalDownloadedBytes + param1;
            this.m_downloadedBytesFromLastCheck = this.m_downloadedBytesFromLastCheck + param1;
            return;
        }// end function

        public function updateStatistic(param1:uint = 0) : void
        {
            param1 = param1 || getTimer();
            if (param1 - this.m_lastCheckTime < UPDATE_STATISTIC_INTERVAL)
            {
                return;
            }
            this.m_downloadBytesPerSecond = this.m_downloadedBytesFromLastCheck / (param1 - this.m_lastCheckTime) * 1000;
            this.m_downloadedBytesFromLastCheck = 0;
            this.m_lastCheckTime = param1;
            if (this.m_downloadBytesPerSecond > 0)
            {
                this.m_maxDownloadSpeed = Math.max(this.m_maxDownloadSpeed, this.m_downloadBytesPerSecond);
                this.m_minDownloadSpeed = Math.min(this.m_minDownloadSpeed, this.m_downloadBytesPerSecond);
                this.m_avgDownloadSpeed = (this.m_maxDownloadSpeed + this.m_minDownloadSpeed) * 0.5;
            }
            return;
        }// end function

        public function get totalDownloadedBytes() : uint
        {
            return this.m_totalDownloadedBytes;
        }// end function

        public function get debug() : Boolean
        {
            return this.m_debug;
        }// end function

        public function get maxDownloadSpeed() : Number
        {
            return this.m_maxDownloadSpeed;
        }// end function

        public function get minDownloadSpeed() : Number
        {
            return this.m_minDownloadSpeed;
        }// end function

        public function get avgDownloadSpeed() : Number
        {
            return this.m_avgDownloadSpeed;
        }// end function

        public function get downloadBytesPerSecond() : Number
        {
            return this.m_downloadBytesPerSecond;
        }// end function

        public static function get instance() : DownloadStatistic
        {
            var _loc_1:* = m_instance || new DownloadStatistic(new SingletonEnforcer());
            m_instance = m_instance || new DownloadStatistic(new SingletonEnforcer());
            return _loc_1;
        }// end function

    }
}

class SingletonEnforcer extends Object
{

    function SingletonEnforcer()
    {
        return;
    }// end function

}

